package com.bac.bacplatform;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

public class LoadingActivity extends BaseActivity {
	private ViewPager viewPager;

	private ArrayList<View> pageViews;
	private ImageView[] imageViews;

	private ViewGroup viewPics;
	private ViewGroup viewPoints;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		LayoutInflater inflater = getLayoutInflater();
		pageViews = new ArrayList<View>();
		pageViews.add(inflater.inflate(R.layout.guide_view_page_1, null));
		pageViews.add(inflater.inflate(R.layout.guide_view_page_2, null));
		pageViews.add(inflater.inflate(R.layout.guide_view_page_3, null));
		pageViews.add(inflater.inflate(R.layout.guide_view_page_4, null));

		viewPics = (ViewGroup) inflater
				.inflate(R.layout.activity_loading, null);
		viewPoints = (ViewGroup) viewPics.findViewById(R.id.viewGroup);
		viewPoints.setVisibility(View.GONE);
		viewPager = (ViewPager) viewPics.findViewById(R.id.guidePages);

		imageViews = new ImageView[pageViews.size()];
		for (int i = 0; i < pageViews.size(); i++) {
			ImageView imageView = new ImageView(this);
			imageView.setLayoutParams(new LayoutParams(20, 20));
			imageView.setPadding(20, 0, 20, 0);
			imageViews[i] = imageView;
			if (i == 0) {
				imageViews[i].setBackgroundResource(R.drawable.indicator_focus);
			} else {
				imageViews[i]
						.setBackgroundResource(R.drawable.indicator_unfocus);
			}
			viewPoints.addView(imageViews[i]);
		}

		setContentView(viewPics);
		viewPager.setAdapter(new GuidePageAdapter());
		viewPager.setOnPageChangeListener(new GuidePageChangeListener());
	}

	private class GuidePageAdapter extends PagerAdapter {
		@Override
		public int getCount() {
			return pageViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(View container, int position) {
			((ViewPager) container).addView(pageViews.get(position));
			pageViews.get(pageViews.size() - 1).findViewById(R.id.loading_end)
					.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View arg0) {
							Intent intent = new Intent(LoadingActivity.this,MainActivity.class);
							startActivityIn(intent);
							finish();
						}
					});
			return pageViews.get(position);
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager) container).removeView(pageViews.get(position));
		}
	}

	private class GuidePageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int position) {
			for (int i = 0; i < imageViews.length; i++) {
				if (i == position) {
					imageViews[i]
							.setBackgroundResource(R.drawable.indicator_focus);
				} else {
					imageViews[i]
							.setBackgroundResource(R.drawable.indicator_unfocus);
				}
			}
		}
	}

	@Override
	public void init() {
		
	}
}
